package main;

import java.util.ArrayList;
import java.util.List;

/**
 * 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数：
 * <p>
 * 之后从左往右，逐行读取字符："PAHNAPLSIIGYIR"
 * 实现一个将字符串进行指定行数变换的函数:
 * string convert(string s, int numRows);
 * 示例 1:
 * 输入: s = "PAYPALISHIRING", numRows = 3
 * 输出: "PAHNAPLSIIGYIR"
 * P   A   H   N
 * A P L S I I G
 * Y   I   R
 * 示例 2:
 * 输入: s = "PAYPALISHIRING", numRows = 4
 * 输出: "PINALSIGYAHRPI"
 * 解释:
 * P     I    N
 * A   L S  I G
 * Y A   H R
 * P     I
 */
public class LeetCode6 {
    public String convert(String s, int numRows) {
        if (numRows == 1)
            return s;
        String[] result_arr = new String[numRows];
        String result = new String();
        boolean addflag = true;
        int j = 0;
        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if (result_arr[j] == null) {
                result_arr[j] = String.valueOf(arr[i]);
            } else {
                result_arr[j] += String.valueOf(arr[i]);
            }
            if (addflag) {
                if (j < (result_arr.length - 1)) {
                    j++;
                } else {
                    j--;
                    addflag = false;
                }
            } else {
                if (j > 0) {
                    j--;
                } else {
                    j++;
                    addflag = true;
                }
            }
        }
        for (String string : result_arr) {
            if (string != null)
                result += string;
        }
        return result;
    }

    public String convert1(String s, int numRows) {  //官方解答1，思路相同，只是实现比我的好看多了

        if (numRows == 1) return s;

        List<StringBuilder> rows = new ArrayList<StringBuilder>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++)
            rows.add(new StringBuilder());

        int curRow = 0;
        boolean goingDown = false;

        for (char c : s.toCharArray()) {
            rows.get(curRow).append(c);
            if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
            curRow += goingDown ? 1 : -1;
        }

        StringBuilder ret = new StringBuilder();
        for (StringBuilder row : rows) ret.append(row);
        return ret.toString();
    }

    public String convert2(String s, int numRows) {  // 官方解答2，考虑过，但是推导不出每行的索引规律

        if (numRows == 1) return s;

        StringBuilder ret = new StringBuilder();
        int n = s.length();
        int cycleLen = 2 * numRows - 2;

        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j + i < n; j += cycleLen) {
                ret.append(s.charAt(j + i));
                if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
                    ret.append(s.charAt(j + cycleLen - i));
            }
        }
        return ret.toString();
    }

    public static void main(String[] args) {
        LeetCode6 leetCode6 = new LeetCode6();
        String s = "AB";
        String result = leetCode6.convert(s, 1);
        System.out.println(result);
    }
}
